module.exports = app =>{
    const data = require('../src/data/list.json')

    app.get('/list',(req,res,next)=>{
        
        res.send({code:1,msg:'',data})
    })

    // 创建
    app.post('/list/add',(req,res,next)=>{
        
        const { submitData } = req.body;

        data.push(submitData)
        
        res.send({code:1,msg:'保存成功',data})
    })
    // 编辑
    app.post( '/list/edit',(req,res,next)=>{
        const { submitData } = req.body;

        let index = data.findIndex(item=>item.id === submitData.id);
        data[index] = {...submitData};

        res.send({code:1,msg:'编辑成功',data})

    })


    // 删除
    app.post( '/list/delete',(req,res,next)=>{
        const { id } = req.body;
        let index = data.findIndex(item=>item.id === id);

        if(index === -1){
            res.send({code:0,msg:'删除失败，未匹配到对应id',data})
            return 
        }

        data.splice(index,1)
        res.send({code:1,msg:'删除成功',data})

    })

    // 待发布变已发布
    app.post( '/list/change',(req,res,next)=>{
        const { id } = req.body;
        let index = data.findIndex(item=>item.id === id);

        if(index === -1){
            res.send({code:0,msg:'发布失败',data})
            return 
        }
        
        // 改变状态
        data[index].status = 'done';
        // 记录发布时间
        data[index].sendTime = new Date().getTime();

        res.send({code:1,msg:'发布成功',data})

    })

  
}
